package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_DuiJie_XuQiuActivity extends Activity {
    /****************** 对接需求详情 *****************/
    private RelativeLayout duijie_xuqiu_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private TextView duijie_xuqiu_bianhao, duijie_xuqiu_title,
            duijie_xuqiu_fenlei, duijie_xuqiu_yusuan, duijie_xuqiu_status,
            duijie_xuqiu_end_time, duijie_lianxi_telephone,
            duijie_lianxi_zuoji, duijie_lianxi_qq, duijie_lianxi_email,
            duijie_lianxi_name;
    private Button button_quxiao_duijie;
    private WebView duijie_xuqiu_details;
    private String tid;
    private LinearLayout liearlayout_lianxi_message;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__dui_jie__xu_qiu);
        // XJApplication.getInstance().addActivity(this);
        duijie_xuqiu_back = (RelativeLayout) findViewById(R.id.duijie_xuqiu_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        duijie_xuqiu_bianhao = (TextView) findViewById(R.id.duijie_xuqiu_bianhao);
        duijie_xuqiu_title = (TextView) findViewById(R.id.duijie_xuqiu_title);
        duijie_xuqiu_fenlei = (TextView) findViewById(R.id.duijie_xuqiu_fenlei);
        duijie_xuqiu_yusuan = (TextView) findViewById(R.id.duijie_xuqiu_yusuan);
        duijie_xuqiu_details = (WebView) findViewById(R.id.duijie_xuqiu_details);
        duijie_xuqiu_status = (TextView) findViewById(R.id.duijie_xuqiu_status);
        duijie_xuqiu_end_time = (TextView) findViewById(R.id.duijie_xuqiu_end_time);
        button_quxiao_duijie = (Button) findViewById(R.id.button_quxiao_duijie);
        liearlayout_lianxi_message = (LinearLayout) findViewById(R.id.liearlayout_lianxi_message);
        duijie_lianxi_telephone = (TextView) findViewById(R.id.duijie_lianxi_telephone);
        duijie_lianxi_zuoji = (TextView) findViewById(R.id.duijie_lianxi_zuoji);
        duijie_lianxi_qq = (TextView) findViewById(R.id.duijie_lianxi_qq);
        duijie_lianxi_email = (TextView) findViewById(R.id.duijie_lianxi_email);
        duijie_lianxi_name = (TextView) findViewById(R.id.duijie_lianxi_name);


        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_duijie_xuqiu();
            }
        });

        if (getIntent().getIntExtra("duijie_details", 0) == 2) {
            button_quxiao_duijie.setText("恢复取消对接");
            button_quxiao_duijie.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    huifu_duijie_xuqiu();
                }
            });

        } else if (getIntent().getIntExtra("duijie_details", 0) == 1) {
            button_quxiao_duijie.setText("取消对接");

            button_quxiao_duijie.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    quxiao_duijie_xuqiu();
                }
            });
        }

        duijie_xuqiu_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_DuiJie_XuQiuActivity.this.finish();
            }
        });
    }

    public void get_duijie_xuqiu() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        parms.addBodyParameter("token", sp.getString("xianji_token", ""));
        parms.addBodyParameter("id", getIntent().getStringExtra("demand_id"));
        httpUtils.send(HttpMethod.POST, Contant.duijie_xiangqing, parms,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                JSONArray array = response.getJSONArray("data");
                                JSONObject obj = array.getJSONObject(0);
                                duijie_xuqiu_title.setText(obj
                                        .getString("title"));
                                duijie_xuqiu_bianhao.setText(obj
                                        .getString("prono"));
                                duijie_xuqiu_fenlei.setText(obj
                                        .getString("name"));

                                if (obj.getString("budget")
                                        .equals("negotiable")) {

                                    duijie_xuqiu_yusuan.setText("面议");
                                } else {
                                    duijie_xuqiu_yusuan.setText(obj
                                            .getString("budget_val"));
                                }

                                duijie_xuqiu_details.loadDataWithBaseURL(null,
                                        obj.getString("details"), "text/html",
                                        "utf-8", null);

                                if (obj.getInt("status") == 1) {
                                    duijie_xuqiu_status.setText("已发布");

                                } else if (obj.getInt("status") == 11) {

                                    duijie_xuqiu_status.setText("待审核");
                                } else if (obj.getInt("status") == 10) {

                                    duijie_xuqiu_status.setText("审核不通过");
                                } else if (obj.getInt("status") == 12) {

                                    duijie_xuqiu_status.setText("已发布");
                                } else if (obj.getInt("status") == 13) {

                                    duijie_xuqiu_status.setText("专家对接中");
                                } else if (obj.getInt("status") == 14) {

                                    duijie_xuqiu_status.setText("专家对接中");
                                } else if (obj.getInt("status") == 15) {

                                    duijie_xuqiu_status.setText("分类不确定");
                                } else if (obj.getInt("status") == 16) {

                                    duijie_xuqiu_status.setText("已发布");
                                } else if (obj.getInt("status") == 17) {
                                    duijie_xuqiu_status.setText("已发布");
                                }
                                if (obj.getString("end_at")
                                        .equals("0000-00-00")) {
                                    duijie_xuqiu_end_time.setText("");
                                } else {
                                    duijie_xuqiu_end_time.setText(obj
                                            .getString("end_at"));
                                }
                                JSONObject obj1 = array.getJSONObject(1);
                                tid = obj1.getString("id");
                                if (obj1.getInt("open") == 0) {
                                    // 隐藏
                                    liearlayout_lianxi_message.setVisibility(View.GONE);
                                } else {
                                    // 显示
                                    liearlayout_lianxi_message
                                            .setVisibility(View.VISIBLE);
                                    duijie_lianxi_telephone.setText(obj1
                                            .getJSONObject("dauthor")
                                            .getString("tel"));
                                    duijie_lianxi_zuoji.setText(obj1
                                            .getJSONObject("dauthor")
                                            .getString("code"));
                                    duijie_lianxi_qq.setText(obj1
                                            .getJSONObject("dauthor")
                                            .getString("qq"));
                                    duijie_lianxi_email.setText(obj1
                                            .getJSONObject("dauthor")
                                            .getString("email"));
                                    duijie_lianxi_name.setText(obj1
                                            .getJSONObject("dauthor")
                                            .getString("name"));
                                }
                            }
                            handler.sendEmptyMessage(1);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
		/*HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("token", sp.getString("xianji_token", ""));
		map.put("id", getIntent().getStringExtra("demand_id"));

		JSONObject params = new JSONObject(map);

		JsonObjectRequest request = new JsonObjectRequest(Method.POST,
				Contant.duijie_xiangqing, params, new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {

						Log.i("zzzzzzzz", response + "");

						try {

							if (response.getInt("status") == 1) {
								JSONArray array = response.getJSONArray("data");
								JSONObject obj = array.getJSONObject(0);
								duijie_xuqiu_title.setText(obj
										.getString("title"));
								duijie_xuqiu_bianhao.setText(obj
										.getString("prono"));
								duijie_xuqiu_fenlei.setText(obj
										.getString("name"));

								if (obj.getString("budget")
										.equals("negotiable")) {

									duijie_xuqiu_yusuan.setText("面议");
								} else {

									duijie_xuqiu_yusuan.setText(obj
											.getString("budget_val"));
								}

								duijie_xuqiu_details.loadDataWithBaseURL(null,
										obj.getString("details"), "text/html",
										"utf-8", null);

								if (obj.getInt("status") == 1) {
									duijie_xuqiu_status.setText("已发布");

								} else if (obj.getInt("status") == 11) {

									duijie_xuqiu_status.setText("待审核");
								} else if (obj.getInt("status") == 10) {

									duijie_xuqiu_status.setText("审核不通过");
								} else if (obj.getInt("status") == 12) {

									duijie_xuqiu_status.setText("已发布");
								} else if (obj.getInt("status") == 13) {

									duijie_xuqiu_status.setText("专家对接中");
								} else if (obj.getInt("status") == 14) {

									duijie_xuqiu_status.setText("专家对接中");
								} else if (obj.getInt("status") == 15) {

									duijie_xuqiu_status.setText("分类不确定");
								} else if (obj.getInt("status") == 16) {

									duijie_xuqiu_status.setText("已发布");
								} else if (obj.getInt("status") == 17) {

									duijie_xuqiu_status.setText("已发布");
								}

								if (obj.getString("end_at")
										.equals("0000-00-00")) {

									duijie_xuqiu_end_time.setText("");
								} else {

									duijie_xuqiu_end_time.setText(obj
											.getString("end_at"));

								}
								JSONObject obj1 = array.getJSONObject(1);
								tid = obj1.getString("id");
								if (obj1.getInt("open") == 0) {
									// 隐藏
									liearlayout_lianxi_message
											.setVisibility(View.GONE);
								} else {
									// 显示
									liearlayout_lianxi_message
											.setVisibility(View.VISIBLE);
									duijie_lianxi_telephone.setText(obj1
											.getJSONObject("dauthor")
											.getString("tel"));
									duijie_lianxi_zuoji.setText(obj1
											.getJSONObject("dauthor")
											.getString("code"));
									duijie_lianxi_qq.setText(obj1
											.getJSONObject("dauthor")
											.getString("qq"));
									duijie_lianxi_email.setText(obj1
											.getJSONObject("dauthor")
											.getString("email"));
									duijie_lianxi_name.setText(obj1
											.getJSONObject("dauthor")
											.getString("name"));
								}

							}

							handler.sendEmptyMessage(1);

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
					}
				});
		XJApplication.getHttpQueues().cancelAll("abcPost");
		request.setTag("abcPost");
		request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
		XJApplication.getHttpQueues().add(request);
		XJApplication.getHttpQueues().start();*/
    }

    /**
     * 取消对接
     */

    public void quxiao_duijie_xuqiu() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        map.put("token", sp.getString("xianji_token", ""));
        map.put("tid", tid);

        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                Contant.quxiao_duijie, params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(Main_DuiJie_XuQiuActivity.this,
                            response.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (response.getInt("status") == 1) {
                        Main_DuiJie_XuQiuActivity.this.finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                /*
                 * if(mCustomProgressDialog!=null){
                 * mCustomProgressDialog.dismiss();
                 *
                 * mCustomProgressDialog=null; }
                 */
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    /**
     * 取消对接
     */

    public void huifu_duijie_xuqiu() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        map.put("token", sp.getString("xianji_token", ""));
        map.put("tid", tid);

        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                Contant.huifu_duijie, params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(Main_DuiJie_XuQiuActivity.this,
                            response.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (response.getInt("status") == 1) {
                        Main_DuiJie_XuQiuActivity.this.finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
